import moment from 'moment'
import { getMusicLyric } from '@/network/blog/music.request'

// 获取文章id
export function getArticleId(props) {
  return props.location.search.split('=').pop()
}

// 获取一个随机数 [Min, Max]
export function RandomNumBoth(Min, Max) {
  const Range = Max - Min
  const Rand = Math.random()
  return Min + Math.round(Rand * Range) //四舍五入
}

// 计算网页运行时间
export function getRunTimer(startTime) {
  const endTime = new Date()
  // const years = moment(endTime).diff(moment(startTime), 'years')
  // const months = moment(endTime).diff(moment(startTime), 'months') % 12
  const days = moment(endTime).diff(moment(startTime), 'days')
  const hours = moment(endTime).diff(moment(startTime), 'hours') % 24
  const minutes = moment(endTime).diff(moment(startTime), 'minutes') % 60
  const seconds = moment(endTime).diff(moment(startTime), 'seconds') % 60
  return `${days}天${hours}时${minutes}分${seconds}秒`
}

// 获取用户id
export function getUserId(props) {
  return props.match.params.id
}

// 添加歌词
export function addLyricToMusic(loveMusics = []) {
  loveMusics.forEach(async (item) => {
    if (item.musicId) {
      const result = await getMusicLyric(item.musicId)
      if (result.data && result.data.lrc) {
        item.lrc = result.data.lrc.lyric
      }
    }
  })
  return loveMusics
}

/**
 * 计算时间差
 * @param date {时间}
 * @returns {string}
 */
export function dateFormat(date) {
  let m2 = moment() //当下时间
  let m1 = moment(date)
  let du = moment.duration(m2 - m1, 'ms') //做差
  let days = du.get('days')
  let hours = du.get('hours')
  let mins = du.get('minutes')
  let ss = du.get('seconds')
  // console.log(days,hours,mins,ss)
  // 输出结果为   01天08时09分40秒
  if (days > 0) {
    date = moment(date).format('YYYY年MM月DD日')
    return date
  } else if (hours > 0) {
    return hours + '小时之前'
  } else if (mins > 0) {
    return mins + '分钟之前'
  } else {
    return ss + '秒之前'
  }
}
